﻿/*
 * 2012  - C.B.S. - Compute Software Solutions
 * This file is part of Observation Log.
 * 
 *     Observation Log is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Observation Log is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Observation Log.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Data;
using System.Windows.Forms;

namespace ObservationLog
{
    public partial class frmObserveLog : Form
    {
        public frmObserveLog()
        {
            InitializeComponent();
        }

        private void ObserveLog_Load(object sender, EventArgs e)
        {
            this.Text = "Observation Log - " + ItemName  + " - " + this.ItemDesc;
            this.lblItemName.Text = this.ItemName;
            this.lblItemType.Text = this.ItemType;
            this.lblItemDesc.Text = this.ItemDesc;
            this.lblItemType.Left = this.lblItemName.Left + this.lblItemName.Width + 5;
            this.lblItemDesc.Left = this.lblItemType.Left + this.lblItemType.Width + 5;
            this.observeLocationsTableAdapter.Fill(this.observationLogDBDataSet.ObserveLocations);
            this.instrumentsViewTableAdapter.Fill(this.observationLogDBDataSet.InstrumentsView);
            this.observeLogTableAdapter.Fill(this.observationLogDBDataSet.ObserveLog);
            if (this.observeLogBindingSource.Count == 0 && !this.AddingNew)
            {
                this.cmbInstruments.Enabled = 
                    this.observeDateDateTimePicker.Enabled = 
                    this.cmbObserveLocation.Enabled = 
                    this.commentsTextBox.Enabled = false;
                this.lblError.Visible = true;               
            }
            else if (this.AddingNew)
            {
                this.observeLogBindingSource.AddNew();
                this.observeLogBindingSource.MoveLast();
                this.cmbInstruments.Enabled = this.observeDateDateTimePicker.Enabled = 
                    this.cmbObserveLocation.Enabled = this.commentsTextBox.Enabled = true;
                this.lblError.Visible = false;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (frmInstruments fi = new frmInstruments())
            {
                if (fi.ShowDialog(this) == DialogResult.OK)
                {
                    if (fi.InstrumentId > 0)
                    {
                        this.instrumentsViewTableAdapter.Fill(this.observationLogDBDataSet.InstrumentsView);
                        this.cmbInstruments.SelectedValue = fi.InstrumentId;
                    }
                }
            }
        }

        private void frmObserveLog_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                DataRowView drv = ((DataRowView)this.observeLogBindingSource.Current);
                if (drv != null && (drv.IsNew || drv.IsEdit))
                {
                    drv["InstrumentId"] = this.cmbInstruments.SelectedValue;
                    drv["ObserveDate"] = this.observeDateDateTimePicker.Value;
                    drv["ObserveLocation"] = this.cmbObserveLocation.Text;
                    drv["Comments"] = this.commentsTextBox.Text;
                }
                this.Validate();
                this.observeLogBindingSource.EndEdit();           
            }
            catch
            {
                if (MessageBox.Show("The new log entry could not be added due to missing data.\n" + 
                    "Do you wish to return to the observation log to add the missing data?", 
                    "Observation Log - Add New",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1) != DialogResult.No)
                {
                    e.Cancel = true;
                    return;
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                    return;
                }
            }
            this.tableAdapterManager.UpdateAll(this.observationLogDBDataSet);
            this.DialogResult = DialogResult.OK;
        }    

        internal int ItemId { get; set; }
        internal string ItemName { get; set; }
        internal string ItemDesc { get; set; }
        internal string ItemType { get; set; }
        internal bool AddingNew { get; set; }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.cmbInstruments.Enabled = this.observeDateDateTimePicker.Enabled = 
                this.cmbObserveLocation.Enabled = this.commentsTextBox.Enabled = true;
            this.lblError.Visible = false;
        }

        private void observeLogBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            DataRowView drv = ((DataRowView)this.observeLogBindingSource.Current);
            if (drv != null && drv.IsNew)
            {
                drv["ItemId"] = this.ItemId;
                drv["ObserveDate"] = DateTime.Now;                    
            }
        }
    }
}
